package com.example.bes1002.base.multiple.ds.demo.config;

import com.bes.enterprise.jdbc.spring.boot.builder.BesDataSourceBuilder;
import com.bes.enterprise.jdbc.spring.boot.builder.BesDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DataSourceConfig {

    @Autowired
    private BESDataSourceProperties1 besDataSourceProperties1;

    @Autowired
    private BESDataSourceProperties2 besDataSourceProperties2;

    @Bean(value = "dataSource1")
    public DataSource dataSource() throws SQLException {
        return BesDataSourceBuilder.create(besDataSourceProperties1).build();
    }

    @Bean(value = "dataSource2")
    public DataSource dataSource2() throws SQLException {
        return BesDataSourceBuilder.create(besDataSourceProperties2).build();
    }

    @Component
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    private class BESDataSourceProperties1 extends BesDataSourceProperties {

    }

    @Component
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    private class BESDataSourceProperties2 extends BesDataSourceProperties {

    }

}
